package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import entity.Courses;

public class CourseDao extends BaseSql<Courses> {
	private Connection conn;

	public CourseDao() {
		conn = DBUtile.getConnection();
	}

	public List<Courses> getCourse(String name) {
		List<Courses> list = new ArrayList<>();
		String sql = "select * from courses where id in(select course_id from majorcourse where major_id in(select id from major WHERE NAME=?))";
		try {
			PreparedStatement pstm = conn.prepareStatement(sql);
			pstm.setString(1, name);
			ResultSet rs = pstm.executeQuery();
			while (rs.next()) {
				Courses cs = new Courses();
				cs.setId(rs.getInt("id"));
				cs.setName(rs.getString("name"));
				list.add(cs);
			}
			return list;

		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}

	}
}
